package com.hoshiicloud.registered.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.hoshiicloud.common.exception.ServiceException;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.utils.SMSCodeUtil;
import com.hoshiicloud.registered.feign.UserService;
import com.hoshiicloud.registered.feign.VericenterService;
import com.hoshiicloud.registered.service.RegisterdUsersService;
import com.hoshiicloud.user.param.company.CompanyAddParam;
import com.hoshiicloud.user.param.company.CompanyRegisteredParam;
import javax.validation.constraints.NotBlank;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author MarkerCCC
 * @date 2019/9/12 - 10:33
 */
@Slf4j
@Service
public class RegisterdUsersServiceImpl implements RegisterdUsersService {

    @Autowired
    private VericenterService vericenterService;

    @Autowired
    private UserService userService;

    private Response verificationCode(String msgId, String code) {
        return vericenterService.checkCode(msgId, code);
    }

    @Override
    public Response<String> getVerificationCode(String mobile) {
        log.info("正在发送验证码");
        String code = SMSCodeUtil.code(4);

        // 发送短信 有效期60秒 数据库验证时间60s
        return vericenterService.sendCode(mobile, code, 300);
    }

    @Override
    public Response theTenantVerification(CompanyRegisteredParam companyRegisteredParam) {
        String msgId = companyRegisteredParam.getMsgId();
        String code = companyRegisteredParam.getCode();
        Response response = this.verificationCode(msgId, code);
        Integer failureCode = -100;
        if (failureCode.equals(response.getCode())) {
            throw new ServiceException(response.getMsg());
        }

        CompanyAddParam companyAddParam = new CompanyAddParam();
        BeanUtil.copyProperties(companyRegisteredParam, companyAddParam, false);
        Response registerResponse = userService.companyRegister(companyAddParam);
        if (!registerResponse.isSuccess()) {
            throw new ServiceException("注册失败");
        }
        return Response.success("注册成功");
    }
}
